home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DEMOS / OTLIGHT.ZIP / LIGHT.DOC < prev    next >
Encoding:
Text File  |  1996-10-18  |  8.6 KB  |  223 lines

  1.  
  2.                      - THE OUTLAW TRIAD DEMO-SERIES -
  3.  
  4. ───────────────────────────────■ PART XII ■───────────────────────────────────
  5.  
  6.                          Written by : Inopia/OT
  7.                          Edited by  : Vulture/OT
  8.                          Topic      : Improved flatshading
  9.  
  10. ──────────────────────────────■ Introduction ■────────────────────────────────
  11.  
  12.  Welcome to the Outlaw Triad demo-series! In these series we will be talking
  13.  about programming demo-effects in either pascal or assembler. Theory behind
  14.  the effects shall be discussed while a full sourcecode is also provided.
  15.  So... it's time to move on to some serious math. We will implement improved
  16.  flatshading now. I (Inopia) got a lot of these things from an article written
  17.  by STO/THECLAN, in Insight#1 magazine, and I read PC Underground. Be sure to
  18.  get them if you want to learn more about programming 3d. Here we go!
  19.  
  20. ─────────────────────────────────■ Theory ■───────────────────────────────────
  21.  
  22.  - VECTORS -
  23.  
  24.  Okay, first we need to discuss vectors. A vector is a kind of arrow, with a
  25.  starting point, a direction and a length. We use 3D vectors, so we define a
  26.  vector with X,Y,Z. A vector is noted like this:
  27.  
  28.       =Vx
  29.      V=Vy
  30.       =Vz
  31.  
  32.  Ok, a triangle has three coordinates. But we need two vectors to do our
  33.  calculations with. Let's define those vectors with some vector substraction:
  34.  
  35.   V1 = P2 - P1
  36.   V2 = P3 - P1
  37.  
  38.  You need to substract all seperate components (x,y,z) from eachother here.
  39.  So x-x, y-y and z-z. P1,P2 and P3 are the 3d triangle coordinates. V1 and
  40.  V2 are the vectors we are going to do some calculating with.
  41.  
  42.  - VECTORS AND LIGHT -
  43.  
  44.  Now we need to find out a vector, in a right angle to the triangle. This is
  45.  needed to determine the color of the face. Picture this: you have a postcard,
  46.  a shiny postcard which you got from your girlfriend, who was on vacation in
  47.  Singapore (without you, cos your car broke down). You hold it into the light.
  48.  If you hold it like this...
  49.  
  50.  1.  |<------------------------------
  51.      |
  52.      |
  53.      |                                 Lightbeam
  54.      |
  55.      |
  56.      |<------------------------------
  57.  
  58.   Postcard
  59.  
  60.  There is a lot of light falling onto the card. But if you hold it like this:
  61.  
  62.  2.  \<------------------------------
  63.       \
  64.        \
  65.         \                              Lightbeam
  66.          \
  67.           \<-------------------------
  68.  
  69.  There is LESS light falling onto the card! Let's find out how we can
  70.  calculate this. Let's bring up that card again:
  71.  
  72.  
  73.    |
  74.    |                                     \
  75.    |                                               V
  76.    |                                       \     /
  77.    |
  78.  --|----------- V                            \ /
  79.    |
  80.    |                                         / \
  81.    |
  82.    |                                       /     \
  83.    |
  84.    |                                               \
  85.  
  86.   1.                                 2.
  87.  
  88.  As you can see I created a line through the middle of the card, IN A RIGHT
  89.  ANGLE TO THE CARD. This line is called the normal. Let's shine some light
  90.  on this, shall we?
  91.  
  92.    |    <----------------
  93.    |                                     \<-------------------
  94.    |
  95.    |                                       \     /
  96.    |
  97.  --|-----------      beam                    \ /
  98.    |
  99.    |                                         / \
  100.    |
  101.    |                                       /     \
  102.    |
  103.    |    <----------------                          \<---------
  104.  
  105.    1.                                 2.
  106.  
  107.  In the first picture, the normal and the lightbeam have THE SAME ANGLE WITH
  108.  THE POSTCARD! This means there's a full amount of light shining on the card.
  109.  In the second picture however, the two angles differ. The angle between the
  110.  lightsource and the normal is larger than zero. If we can find this angle
  111.  (between the normal and the lightsource) we can find out how much light is
  112.  shining onto the postcard, and in our case, the polygon in 3d space!!!
  113.  
  114.  - CROSSPRODUCT - DOTPRODUCT -
  115.  
  116.  To find the normal, we must we take the crossproduct of the two vectors we
  117.  calculated before. This will leave us with just one vector. This vector is
  118.  a right angle with the two other vectors, and thus in a right angle with the
  119.  triangle.
  120.  
  121.   V3 = V1 * V2
  122.  
  123.   V3x = V1y * V2z - V1z * V2y
  124.   V3y = V1z * V2x - V1x * V2z
  125.   V3z = V1x * V2y - V1y * V2x
  126.  
  127.  There is just one slight problem. These values can vary in size, according
  128.  to the size of the triangle. We want to get em to be in -1 to 1, so that
  129.  we can adjust the size to anything we want. As said before, a vector also
  130.  has a size. One can easily calulate the size of a vector like this:
  131.  
  132.   length = sqrt (V3x * V3x + V3y * V3y + V3z * V3z)
  133.  
  134.  To shrink a vector to a length of 1, we devide all components of the vector
  135.  by the length of the vector:
  136.  
  137.   V3x = V3x / length
  138.   V3y = V3y / length
  139.   V3z = V3z / length
  140.  
  141.  We also multiply these values with the maximum number of colors you reserved
  142.  for the shading. Check the source to see what I mean. Anyhow, V3 holds the
  143.  normalvector of the triangle.
  144.  
  145.  Now we need to come up with a lightsource. Well, the lightsource is easy:
  146.  it's defined by you. Mostly, the light is coming from the viewer, so the
  147.  lightsource would be:
  148.  
  149.         0
  150.     LS  0
  151.        -1
  152.  
  153.  You see? This is a 3d vector also. Now, one more formula coming up...
  154.  The dotproduct. This formula returns a value which is equal to the cosine
  155.  of the angle between the lightsource and the triangle. You use the normal
  156.  you just calculated and the lightvector for the calculation.
  157.  
  158.   DP = V3x * LSx + V3y * LSy + V3z * LSz
  159.  
  160.  Now, if we take the dotproduct of the normal and the lightvector like
  161.  stated here, we've calculated the exact color of the face. Draw it and
  162.  you're done! Phew! Pretty complicated math but well worth the effort.
  163.  Again, check out the source to see how it can be done.
  164.  
  165.  This method is a lot better than just using the avarage z-value of a poly
  166.  to calculate the color. You can use this method for more complicated objects
  167.  as well. If you know how to handle 3DS objects, you could try this method
  168.  on those objects. Go for it!
  169.  
  170.  - OPTIMIZATION TIPS -
  171.  
  172.  - Well, as you might have guessed, it's impossible to get an engine running
  173.    fast when you've got all those SQRT's to execute. The solution: calculate
  174.    all facenormals once, and then just rotate 'em just like the vertices.
  175.  - There are faces you can't see. Ofcourse you don't have to draw them. Here's
  176.    a neat trick for that: whenever the Z-value of a facenormal is smaller than
  177.    zero, don't draw it! Makes the engine running a LOT faster.
  178.    (note: we will implement this effect in a future trainer)
  179.  - How can we avoid the lightsource vector and the dotproduct? Like this, we
  180.    take the Z-value of the facenormal, and adjust it a bit, so that it will
  181.    fit into the amount of colors you want. The only problem is you can't have
  182.    a variable lightsource. If you still wanna have that effect, try rotating
  183.    the normal vectors over other angles than the vertices, or don't rotate
  184.    the vertices at all (although I think an object that's not moving looks
  185.    very DULL!).
  186.  
  187.  There are probably some untrue things in here so if you know better mail me.
  188.  (mail me or OT for any other reason). Catch ya later!
  189.  
  190.        -Inopia/Outlaw Triad-
  191.  
  192. ───────────────────────────────■ Distro Sites ■───────────────────────────────
  193.  
  194.  Call our distribution sites! All our releases are available at:
  195.  
  196.   BlueNose      World HQ          +31 (0)345-619401
  197.   The Force     Distrosite        +31 (0)36-5346967
  198.   Bugs'R'Us     Distrosite        +31 (0)252-686092    More distros wanted!
  199.   The 7 Angels  Distrosite        +31 (0)715-148377    (preferably outside
  200.   ShockWave     South African HQ  +27 (011)888-6345     of the Netherlands)
  201.   Society HQ    United States HQ  +1  (518)465-6721
  202.   ACe World     Brazilian HQ      +55 (21)-259-8847
  203.   Corps Elite   Canadian HQ      +403 (ITS)-PRIVATE
  204.  
  205.  Also check the major FTP/WWW sites for Outlaw Triad productions.
  206.  
  207. ──────────────────────────────────■ Contact ■─────────────────────────────────
  208.  
  209.  Want to contact Outlaw Triad for some reason? You can reach us at our
  210.  distrosites in Holland. Or if you have e-mail access, mail us:
  211.  
  212.    Vulture   (coder/pr)   comma400@tem.nhl.nl
  213.    Inopia    (coder)      inopia@horizon.nl
  214.  
  215.  Our internet homepage:
  216.  
  217.    http://www.tem.nhl.nl/~comma400/vulture.html
  218.  
  219.  These internet adresses should be valid at least till june 1997.
  220.  
  221. ──────────────────────────────────────────────────────────────────────────────
  222.  
  223.   Quote: Time is the best teacher. Unfortunately, it kills all its students.